package com.maya.medicalwastes;

import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.maya.medicalwastes.entity.AuthOrgEntity;
import com.maya.medicalwastes.entity.CollectBean;
import com.maya.medicalwastes.entity.MenuEntity;
import com.maya.medicalwastes.entity.QueryNurseInfoBean;
import com.maya.medicalwastes.entity.UserEntity;
import com.maya.medicalwastes.httpNet.ApiService;
import com.maya.medicalwastes.httpNet.HttpService;
import com.maya.medicalwastes.httpNet.callback.ResponseCallBack;
import com.maya.medicalwastes.httpNet.common.SharedPreferencesConstant;
import com.maya.medicalwastes.httpNet.manage.RetrofitManager;
import com.maya.medicalwastes.httpNet.model.ResultMainHomeModel;
import com.maya.medicalwastes.httpNet.model.ResultModel;
import com.maya.medicalwastes.ui.DemoActivity;
import com.maya.medicalwastes.util.CustomProber;
import com.maya.medicalwastes.util.MyUtils;
import com.maya.medicalwastes.util.PrefUtils;
import com.maya.medicalwastes.util.ToastUtils;
import com.maya.medicalwastes.util.UsbManagerUtil;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import timing.ukulele.serialport.driver.UsbSerialDriver;
import timing.ukulele.serialport.driver.UsbSerialProber;

/**
 * @author created by fuguoyong
 * 版本：1.0
 * 创建日期：2020-11-19 15
 * 描述：DoctorActivity
 */
public class DoctorActivity extends AppCompatActivity {

    @BindView(R.id.edt_scan)
    EditText mEditText;
    private int baudRate = 9600;
    private boolean withIoManager = true;
    private UsbDevice mDevice;
    @BindView(R.id.tv_title)
    TextView tv_title;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor);
        ButterKnife.bind(this);
        tv_title.setText("扫描科室交接护士");
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.toString().contains("=")) {
                    queryNurseInfo(editable.toString());
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        UsbDevice mDevices = UsbManagerUtil.getDevices(this);
        if (mDevices != null) {
            mDevice = mDevices;
        }
    }

    public void queryNurseInfo(String code) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("codeInformation", code);
        param.put(ParamConstant.TOKEN, PrefUtils.getString(this, SharedPreferencesConstant.TOKEN, ""));
        Observable<ResultModel<QueryNurseInfoBean>> call = RetrofitManager.getInstance().getRetrofit().create(ApiService.class).queryNurseInfo(param);
        HttpService.getInstance().enqueue(call, new ResponseCallBack<ResultModel<QueryNurseInfoBean>>() {
            @Override
            public void onResponse(ResultModel<QueryNurseInfoBean> response) {
                if (response != null && response.httpCode == 200) {
                    if (mDevice == null) {
                        ToastUtils.ViewToast(DoctorActivity.this, "请检查蓝牙秤的连接");
                        return;
                    }
                    Bundle bundle = new Bundle();
                    bundle.putInt("device", mDevice.getDeviceId());
                    bundle.putInt("port", 0);
                    bundle.putInt("baud", baudRate);
                    bundle.putBoolean("withIoManager", withIoManager);
                    bundle.putString("ID", response.data.getID());
                    bundle.putString("dept", response.data.getDept());
                    bundle.putString("userName", response.data.getUserName());
                    MyUtils.IntentToOther(DoctorActivity.this, CollectActivity.class, bundle);
                    finish();
                } else {
                }
            }

            @Override
            public void onFailure(int httpCode) {
                mEditText.setText("");
            }
        }, this);


    }

}
